﻿using System;
using System.Linq.Expressions;
using Glue.Internals;
using Xunit;

namespace GlueTests
{
    public class CompiledExpressionTest
    {
        public class Person
        {
            public PersonAddress Address { get; set; }
            public PersonAddress GetAddress() { return new PersonAddress {Zip = new ZipCode {TheZipper = "1234"}};}
        }
        public class PersonAddress { public ZipCode Zip { get; set; } }
        public class ZipCode { public String TheZipper { get; set; } }

        [Fact]
        public void Should_build_name_correctly()
        {
            Expression<Func<Person, object>> expression = x => x.Address.Zip.TheZipper;
            var compiledExpression = new CompiledExpression(((MemberExpression)expression.Body).Expression);
            Assert.Equal("Person.Address.Zip", compiledExpression.CallPath);
        }

        [Fact]
        public void Should_build_name_correctly_with_methods_in_structure()
        {
            Expression<Func<Person, object>> expression = x => x.GetAddress().Zip.TheZipper;
            var compiledExpression = new CompiledExpression(((MemberExpression)expression.Body).Expression);
            Assert.Equal("Person.GetAddress().Zip", compiledExpression.CallPath);
        }

        [Fact]
        public void Should_get_nearest_parent()
        {
            Expression<Func<Person, object>> expression = x => x.GetAddress().Zip.TheZipper;
            var compiledExpression = new CompiledExpression(((MemberExpression)expression.Body).Expression);
            var maybeParent = compiledExpression.GetNearestParent(new Person());
            Assert.Equal(new Person().GetAddress().Zip.TheZipper, ((ZipCode)maybeParent.GetValue()).TheZipper);
        }
    }
}
